package 蓝桥算法;

public class day01 {
    public int secrch1(int[] nums,int target){//左闭右闭
        int left=0;
        int right=nums.length-1;
        if(target<nums[0]||target>nums[nums.length-1]){//不存在区间
            return -1;
        }
        while (left<=right){
            int middle=(left)+((right-left)>>1);//有效求取中间数，在循环内部,防止数据溢出
            if(nums[middle]>target){
                right=middle-1;
            }
            else if(nums[middle]<target){
                left=middle+1;
            }
            else {
                return middle;
            }
        }
        return -1;
    }
    public int search2(int[] nums,int target){//左闭右开
        int left=1;
        int right=nums.length;
        if(target<nums[0]||target>nums[nums.length-1]){
            return -1;
        }
        while(left<right){
            int middle=(left)+((right-left)>>1);//中间取值放中间
            if(nums[middle]>target){
                right=middle;
            }
            else if(nums[middle]<target){
                left=middle+1;
            }}
        return -1;
    }
    public boolean removeElement(int[] nums,int val){//双指针法
        int slow=0;
        for(int front=0;front<nums.length;front++){
            if(nums[front]!=val){
                nums[slow++]=nums[front];
            }
        }
        return true;
    }
    public static void main(String[] args) {
       int a[]={1,2,4,5,6,7,44,66,77};
       day01 b=new day01();
       int x=b.secrch1(a,4);
        System.out.println(x);
        int y=b.search2(a,44);
        System.out.println(y);
        boolean b1=b.removeElement(a,4);
        for (int i = 0; i < a.length-1; i++) {
            System.out.println(a[i]);
        }
    }
}
